//
//  ZDMainViewController.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/26.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class ZDMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
//        tabBar = ZDTabBar()   //tabBar属性只读 不能赋值  考虑使用KVC赋值
        
//        //KVC设置自定义tabBar
//        setValue(ZDTabBar(), forKey: "tabBar")
        
        
        //设置tabBar的tintColor 该属性会将tabBar中的item图片全部渲染成橙色 可以不用设置选中状态的按钮
        //当然这只是一种巧合  这里关掉了tabBarItem中文本以及选中图片的设置
        tabBar.tintColor = UIColor.orangeColor()
        
        
        let homeVC = ZDHomeViewController()
        addChildViewController(homeVC, title: "首页", normalImageName: "tabbar_home")
//        addChildViewController(homeVC, title: "首页", normalImageName: "tabbar_home", selectedImageName: "tabbar_home_highlighted")

        let messageVC = ZDMessageViewController()
        addChildViewController(messageVC, title: "消息", normalImageName: "tabbar_message_center")
//        addChildViewController(messageVC, title: "消息", normalImageName: "tabbar_message_center", selectedImageName: "tabbar_message_center_highlighted")
        
        //随便创建一个ViewController 添加到tabBarController中 目的是在系统tabBar中占位 为中间按钮腾出空间
        let composeVC = UIViewController()
        addChildViewController(composeVC, title: "", normalImageName: "", selectedImageName: "bb")   //图片名字随意取的，如果赋值为""，那么通过UIImage来获取图片时 会在控制台报错CUICatalog: Invalid asset name supplied  这里在抽取出来的方法内部过滤
        
        let discoveryVC = ZDDiscoveryViewController()
        addChildViewController(discoveryVC, title: "发现", normalImageName: "tabbar_discover")
//        addChildViewController(discoveryVC, title: "发现", normalImageName: "tabbar_discover", selectedImageName: "tabbar_discover_highlighted")
        
        let profileVC = ZDProfileViewController()
        addChildViewController(profileVC, title: "我", normalImageName: "tabbar_profile")
//        addChildViewController(profileVC, title: "我", normalImageName: "tabbar_profile", selectedImageName: "tabbar_profile_highlighted")

    /*
        //设置中间按钮的frame 并添加到tabBar中 ———— 从view的层次结构来看，添加进去的button处在tabBar的最底层，没法响应事件
        let width = tabBar.bounds.width / 5
        let height = tabBar.bounds.height
//        composeButton.frame = CGRectMake(2 * width, 0, width, height)    //OC方式
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: height)//swift方式
        tabBar.addSubview(composeButton)
    */
        
    }
    
    
    //MARK: - 在viewWillAppear方法中添加中间按钮 保证按钮处于tabBar的最顶层
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        
        //设置中间按钮的frame 并添加到tabBar中 ———— 从view的层次结构来看，添加进去的button处在tabBar的最底层，没法响应事件
        let width = tabBar.bounds.width / 5
        let height = tabBar.bounds.height
        //        composeButton.frame = CGRectMake(2 * width, 0, width, height)    //OC方式
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: height)//swift方式
        tabBar.addSubview(composeButton)
    }
    
    
    //MARK: - 添加子控制器  抽取方法
    func addChildViewController(childController: UIViewController, title: String, normalImageName: String) {
        
        //设置navigationBar和tabBar的title
        childController.title = title
        
        //设置tabBar的image
        childController.tabBarItem.image = UIImage(named: normalImageName == "" ? " " : normalImageName)
        
        addChildViewController(UINavigationController(rootViewController: childController))
    }
    
    
    //MARK: - 添加子控制器  抽取方法
    func addChildViewController(childController: UIViewController, title: String, normalImageName: String, selectedImageName: String) {
        
        //设置navigationBar和tabBar的title
        childController.title = title
        //设置tabBarItem选中的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        //设置tabBar的image
        childController.tabBarItem.image = UIImage(named: normalImageName == "" ? " " : normalImageName)
        
        let homeSelectImage = UIImage(named: selectedImageName == "" ? " " : selectedImageName)
        //设置tabBarItem选中的image  由于系统默认将image渲染成蓝色 所以设置image的RenderingMode为AlwaysOriginal
        childController.tabBarItem.selectedImage = homeSelectImage?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        addChildViewController(UINavigationController(rootViewController: childController))
    }
    
    
    //MARK: - 中间按钮点击事件回调方法
    func composeButtonClick() {
    
//        print("点击了中间按钮---\(__FUNCTION__)")
        
        let vc = ZDComposeViewController()
        
        let nav = UINavigationController(rootViewController: vc)
        
        self.presentViewController(nav, animated: true, completion: nil)
    }
    
    
    //MARK: - 懒加载  中间按钮不希望外界使用
    private lazy var composeButton: UIButton = {
        
        let composeButton = UIButton()
        
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        composeButton.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        composeButton.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加点击事件
        composeButton.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return composeButton
    }()

}
